package com.song.controller;

import com.github.pagehelper.PageInfo;
import com.song.pojo.Game;
import com.song.pojo.GameQueryVO;
import com.song.pojo.ResultVO;
import com.song.service.GameService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**比赛的控制层
 * @author song
 */
@Controller
@ResponseBody
@RequestMapping("game")
public class GameController {
    @Resource
    private GameService gameService;

    //Date类型转换
    @InitBinder
    protected void initDateFormatBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }
    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<Game> queryGames(Integer pageNum, Integer pageSize, GameQueryVO vo){
        if (pageNum == null || pageNum < 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 0){
            pageSize = 5;
        }
        PageInfo<Game> gamePageInfo = gameService.queryGames(pageNum, pageSize, vo);
        return new ResultVO<>(gamePageInfo);
    }

    /**
     * 比赛新增
     * @param game
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResultVO<Game> insertGame(Game game){
        if (!game.getHomeTeamId().equals(game.getVisitingTeamId())){
            Integer integer = gameService.insertGame(game);
            if (integer == 1){
                return new ResultVO<>();
            }
            return new ResultVO<>(500,"网络繁忙，稍后再试");
        }
        return new ResultVO<>(400,"主客场球队不能相同");
    }

    /**
     * 通过主键查询比赛
     * @param gameId
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVO<Game> queryByGameId(@PathVariable("id") int gameId){
        Game game = gameService.queryByGameId(gameId);
        if (game != null){
            return new ResultVO<>(game);
        }
        return new ResultVO<>(500,"网络卡顿");
    }

    /**
     * 比赛的更新
     * @param gameId
     * @param game
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVO updateGame(@PathVariable("id") int gameId,Game game){
        game.setGameId(gameId);
        if (!game.getHomeTeamId().equals(game.getVisitingTeamId())) {
            Integer integer = gameService.updateGame(game);
            if (integer == 1) {
                return new ResultVO<>();
            }
            return new ResultVO<>(500, "网络繁忙，稍后再试");
        }
        return new ResultVO<>(400,"主客场球队不能相同");
    }
    /**
     * 删除的更新
     * @param gameId
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVO deleteGame(@PathVariable("id") int gameId){
        Integer integer = gameService.deleteGame(gameId);
        if (integer == 1) {
            return new ResultVO<>();
        }
        return new ResultVO<>(500, "网络繁忙，稍后再试");
    }
}
